package com.andyliu.sys.service.impl;

import com.andyliu.sys.dto.ZtreeNodeDto;
import com.andyliu.sys.dto.ZtreeNodeDto_;
import com.andyliu.sys.mapper.SResourceMapper;
import com.andyliu.sys.mapper.SRoleMapper;
import com.andyliu.sys.mapper.SRoleresourceMapper;
import com.andyliu.sys.pojo.SRole;
import com.andyliu.sys.pojo.SRoleExample;
import com.andyliu.sys.pojo.SRoleresource;
import com.andyliu.sys.pojo.SRoleresourceExample;
import com.andyliu.sys.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName: RoleServiceImpl
 * Description:
 * date: 2021/1/20 5:14 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private SRoleMapper mapper;
    @Autowired
    private SResourceMapper resourceMapper;
    @Autowired
    private SRoleresourceMapper roleresourceMapper;
    @Override
    public Boolean saveRole(SRole role) {
        int flag =0;
        if(role.getId()!=null && role.getId()!=0){
            flag = mapper.updateByPrimaryKeySelective(role);
        }else{
            flag = mapper.insertSelective(role);
        }
        return flag==1?true:false;
    }

    @Override
    public Boolean deleteRoleById(Integer id) {
        int flag = mapper.deleteByPrimaryKey(id);
        return flag==1?true:false;
    }

    @Override
    public SRole selectRoleById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SRole> selectRoleByPage(SRole role, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SRoleExample ex = new SRoleExample();
        SRoleExample.Criteria criteria = ex.createCriteria();
        if(role!=null){
            if(role.getRolename()!=null && !"".equals(role.getRolename())){
                criteria.andRolenameLike("%"+role.getRolename()+"%");
            }
            if(role.getStatus()!=null){
                criteria.andStatusEqualTo(role.getStatus());
            }
        }
        List<SRole> roles = mapper.selectByExample(ex);

        return new PageInfo<SRole>(roles);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Boolean deleteRoleById(Integer[] ids) {
        int flag = 0;
        for(Integer id:ids){
            flag+= mapper.deleteByPrimaryKey(id);
        }
        return flag==ids.length?true:false;
    }

    @Override
    public List<SRole> queryRoles() {
        return mapper.selectByExample(null);
    }
    @Override
    public List<String> selectPermission(Integer roleId) {
        return mapper.selectRolePermission(roleId);
    }

    @Override
    public List<ZtreeNodeDto_> selectRoleResources_(Integer roleId) {
        List<ZtreeNodeDto_> result = resourceMapper.selectResourcesByRoleId(roleId);
        for (ZtreeNodeDto_ ztreeNodeDto_ : result) {
            List<ZtreeNodeDto> children_ = ztreeNodeDto_.getChildren();
            if(children_==null || children_.size()==0) {
                ztreeNodeDto_.setChildren(null);
            }else {
                for (ZtreeNodeDto ztreeNodeDto_2 : children_) {
                    if(ztreeNodeDto_2.getChildren()==null || ztreeNodeDto_2.getChildren().size()==0) {
                        ztreeNodeDto_2.setChildren(null);;
                    }
                }
            }

        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Boolean saveRolePermission(Integer roleId, Integer[] resourceIds) {
       // 删除权限
        SRoleresourceExample ex1 = new SRoleresourceExample();
        SRoleresourceExample.Criteria criteria = ex1.createCriteria();
        criteria.andRoleidEqualTo(roleId);
        roleresourceMapper.deleteByExample(ex1);
        // 保存当前权限
        int flag = 0;
        for(Integer resourceId:resourceIds){
            SRoleresource sRoleresource = new SRoleresource();
            sRoleresource.setRoleid(roleId);
            sRoleresource.setResourceid(resourceId);
            flag+=roleresourceMapper.insertSelective(sRoleresource);
        }
        return flag==resourceIds.length?true:false;
    }

}
